package com.shycloud.mido.member.controller;


import com.shycloud.mido.common.core.constant.CommonConstants;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.common.security.util.SecurityUtils;
import com.shycloud.mido.member.api.entity.MultiClassroom;
import com.shycloud.mido.member.api.vo.AddStudentInClassRoomVO;
import com.shycloud.mido.member.service.MultiClassroomService;
import com.shycloud.mido.member.service.MultiStudentClassroomLinkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import java.util.Map;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author baiqiang
 * @since 2021-07-12
 */
@RestController
@AllArgsConstructor
@Api(tags = "教室操作学生")
@RequestMapping("/v1/multi-student-classroom-link")
public class MultiStudentClassroomLinkController {

	/** 教室内学生操作 service */
	private MultiStudentClassroomLinkService studentService;

	private MultiClassroomService multiClassroomService;

	@ApiOperation("后台管理-添加学生到教室")
	@PostMapping("addStudent")
	public R addStudent(@RequestBody AddStudentInClassRoomVO addStudentInClassRoomVO) {
		return new R(studentService.addStudent(addStudentInClassRoomVO));
	}

	@ApiOperation(value = "APP-批量添加已有学生到教室")
	@PostMapping("addStudentBatch")
	public R addStudentBatch(@RequestBody List<AddStudentInClassRoomVO> addStudentInClassRoomVOs) {

		for (AddStudentInClassRoomVO addStudentInClassRoomVO : addStudentInClassRoomVOs) {
			studentService.addStudent(addStudentInClassRoomVO);
		}

		return new R(true);
	}

	@ApiOperation("后台管理-获取学生列表")
	@GetMapping("getStudentListByRoomId")
	public R getStudentListByRoomId(String classroomId) {
		return new R(studentService.getStudentListByRoomId(classroomId));
	}

	@ApiOperation(value = "后台管理-APP-批量移除学生", notes = "学生ID数组---> studentIds:{[***],[***]}")
	@PostMapping("delStudent")
	public R delStudent(@RequestBody Map<String, Object> reqMap) {

		// 学生ID集合
		List<String> studentIds = (List<String>) reqMap.get("studentIds");
		// 班级ID
		String classroomId = (String) reqMap.get("classroomId");
		// 是否学生主动退出班级
		String isOneself = (String) reqMap.get("isOneself");
		// 获取班级信息
		MultiClassroom classroom = multiClassroomService.getById(classroomId);

		if (!StringUtils.equals("1", isOneself)) {
			if (SecurityUtils.getMember() != null) {
				if (classroom != null && !SecurityUtils.getMember().getId().equals(classroom.getCreateUser())) {
					return new R<Boolean>(CommonConstants.FAIL, "此教师不是教室创建者,无法删除学生", false);
				}
			}
		}

		for (String studentId : studentIds) {
			studentService.delStudent(studentId, classroomId);
		}
		return new R(true);

	}

	@GetMapping("getStudentList")
	@ApiOperation("APP-通讯录列表接口")
	public R getStudentList(String teacherId, String studentName) {
		return new R(studentService.getStudentList(teacherId, studentName));
	}

}
